package com.tcfvic.client.panels.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.tcfvic.client.PersonService;
import com.tcfvic.client.PersonServiceAsync;
import com.tcfvic.client.panels.widgets.addons.advancedtable.AdvancedTable;
import com.tcfvic.client.panels.widgets.addons.advancedtable.RowSelectionListener;
import com.tcfvic.client.pojos.Industry;
import com.tcfvic.client.tools.State;

public class IndustryDetailsInputWidget extends FlexTable implements
		RowSelectionListener {

	private TextBox industryID = new TextBox();
	private TextBox nameTextBox = new TextBox();
	private IndustryDetailsListWidget list = null;

	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);

	public IndustryDetailsInputWidget() {
		super();

		// Create a table to layout the form options
		setCellSpacing(2);
		setWidth("100%");

		// Add some standard form options
		int rowNum = 0;
		setHTML(rowNum, 0, "Industry name");
		setWidget(rowNum, 1, nameTextBox);

		final Button saveButton = new Button("Save");
		saveButton.getElement().setId("saveButton");
		setWidget(rowNum, 3, saveButton);

		// Add a handler to close the DialogBox
		saveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saveIndustryDetails();
			}
		});

	}

	private boolean minimumInput() {
		if (nameTextBox.getText() == null
				|| nameTextBox.getText().trim().length() == 0) {
			new SimpleMessagePopup("Please enter a Industry name", nameTextBox)
					.showPopup();
			return false;
		}
		return true;
	}

	public void createNewRecord() {
		clearAllFields();
		industryID.setText("");
	}
	
	public void clearAllFields() {
		industryID.setText("-1");
		nameTextBox.setText("");
	}

	public ListBox getStatesListBox() {
		final ListBox dropBox = new ListBox(false);

		dropBox.addItem("");
		for (State state : State.values()) {
			dropBox.addItem(state.getState());
		}
		return dropBox;
	}

	@Override
	public void onRowSelected(AdvancedTable sender, String rowId) {
		clearAllFields();
		personService.getIndustryDetails(Integer.valueOf(rowId),
				new AsyncCallback<Industry>() {
					@Override
					public void onSuccess(Industry industry) {
						industryID.setText(String.valueOf(industry.getId()));
						nameTextBox.setText(industry.getName());
					}

					@Override
					public void onFailure(Throwable caught) {
						System.err
								.println("Error occured returning the Industry details: "
										+ caught);
						new SimpleMessagePopup(
								"The Industry record was not retrieved due to an internal error",
								null).showPopup();
					}
				});
	}

	private void saveIndustryDetails() {
		try {
			if (!minimumInput()) {
				return;
			}

			Industry industry = new Industry();
			if (industryID != null && industryID.getText().trim().length() > 0) {
				industry.setId(new Integer(industryID.getText()));
			}

			industry.setName(nameTextBox.getText());

			personService.saveIndustryDetails(industry, new AsyncCallback<Void>() {
				@Override
				public void onSuccess(Void result) {
					clearAllFields();
					if (list != null) {
						list.updateTableData();
					}
					new SimpleMessagePopup("Industry saved successfully", null)
							.showPopup();
				}

				@Override
				public void onFailure(Throwable caught) {
					System.err
							.println("Error occured writing Industry details to database in "
									+ "CompanyDetailsInputWidget.saveIndustryDetails: "
									+ caught);
					new SimpleMessagePopup(
							"The data was not saved due to an internal error",
							null).showPopup();
				}
			});
		} catch (Exception e) {
			new SimpleMessagePopup("An error occured saving the Industry details: "
					+ e, null).showPopup();
		}
	}

	public void addIndustryDetailsList(IndustryDetailsListWidget list) {
		this.list = list;
	}
}
